// ForceIpDlg.cpp : 实现文件
// implementation file

#include "stdafx.h"
#include "MultiCamerasTool.h"
#include "ForceIpDlg.h"

#include <sstream>

extern ::CString getParseLanguageString(::CString strOrl);
// CForceIpDlg 对话框
// CForceIpDlg dialog

IMPLEMENT_DYNAMIC(CForceIpDlg, CDialog)

CForceIpDlg::CForceIpDlg(CWnd* pParent)
	: CDialog(CForceIpDlg::IDD, pParent)
{

}

CForceIpDlg::~CForceIpDlg()
{
}

BOOL CForceIpDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// 根据目前配置的语言初始化界面文本显示 | Initialize the text display of the interface according to the currently configured language

	GetDlgItem(IDC_NETWORK_IMFORMATION)->SetWindowText(getParseLanguageString(_T("_CFGFORM_NETCARD_IMFORMATION")));
	GetDlgItem(IDC_NETWORK_MAC)->SetWindowText(getParseLanguageString(_T("_CFGFORM_NETCARD_MAC")));
	GetDlgItem(IDC_NETWORK_GATEWAY)->SetWindowText(getParseLanguageString(_T("_CFGFORM_NETCARD_GATEWAY")));
	GetDlgItem(IDC_NETWORK_SUBMASK)->SetWindowText(getParseLanguageString(_T("_CFGFORM_NETCARD_SUBMASK")));
	GetDlgItem(IDC_NETWORK_IP)->SetWindowText(getParseLanguageString(_T("_CFGFORM_NETCARD_IP")));
	GetDlgItem(IDC_CAMERA_IMFORMATION)->SetWindowText(getParseLanguageString(_T("_CFGFORM_CAMERA_IMFORMATION")));
	GetDlgItem(IDC_CAMERA_MAC)->SetWindowText(getParseLanguageString(_T("_CFGFORM_CAMERA_MAC")));
	GetDlgItem(IDC_CAMERA_GATEWAY)->SetWindowText(getParseLanguageString(_T("_CFGFORM_CAMERA_GATEWAY")));
	GetDlgItem(IDC_CAMERA_SUBMASK)->SetWindowText(getParseLanguageString(_T("_CFGFORM_CAMERA_SUBMASK")));
	GetDlgItem(IDC_CAMERA_IP)->SetWindowText(getParseLanguageString(_T("_CFGFORM_CAMERA_IP")));
	GetDlgItem(IDOK)->SetWindowText(getParseLanguageString(_T("_CFGFORM_OK")));
	GetDlgItem(IDCANCEL)->SetWindowText(getParseLanguageString(_T("_CFGFORM_CANCEL")));
	initNetAddressInfo();

	// 控制控件状态
	// Control control status
	m_editInterfaceMac.EnableWindow(FALSE);
	m_ipctrlInterfaceGateway.EnableWindow(FALSE);
	m_ipctrlInterfaceMask.EnableWindow(FALSE);
	m_ipctrlInterfaceIp.EnableWindow(FALSE);
	m_editCameraMac.EnableWindow(FALSE);

	return TRUE;
}

void CForceIpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS_INTERFACE_GATEWAY, m_ipctrlInterfaceGateway);
	DDX_Control(pDX, IDC_IPADDRESS_INTERFACE_MASK, m_ipctrlInterfaceMask);
	DDX_Control(pDX, IDC_IPADDRESS_INTERFACE_IP, m_ipctrlInterfaceIp);
	DDX_Control(pDX, IDC_IPADDRESS_CAMERA_GATEWAY, m_ipctrlCameraGateway);
	DDX_Control(pDX, IDC_IPADDRESS_CAMERA_MASK, m_ipctrlCameraMask);
	DDX_Control(pDX, IDC_IPADDRESS_CAMERA_IP, m_ipctrlCameraIp);
	DDX_Control(pDX, IDC_EDIT_INTERFACE_MAC, m_editInterfaceMac);
	DDX_Control(pDX, IDC_EDIT_CAMERA_MAC, m_editCameraMac);
}

void CForceIpDlg::setMvCamera(const Dahua::CMvCameraPtr& sptrMvCamera)
{
	m_sptrMvCamera = sptrMvCamera;
}

void CForceIpDlg::initNetAddressInfo()
{
	if ( NULL == m_sptrMvCamera )
	{
		return;
	}

	int szIp[4] = {0};
	Dahua::CMvCamera::NetAddressInfoPtr sptrGigeInterfaceInfo = m_sptrMvCamera->getGigEInterfaceInfo();
	if ( NULL == sptrGigeInterfaceInfo )
	{
		return;
	}

	// 网卡Mac
	// network card Mac
	m_editInterfaceMac.SetWindowText((LPCTSTR)sptrGigeInterfaceInfo->_strMac.c_str());

	// 网卡Gateway
	// network card gateway
	strIpToNumberIp(sptrGigeInterfaceInfo->_strGatewayAddr.c_str(), szIp, 4);	
	m_ipctrlInterfaceGateway.SetAddress(szIp[0], szIp[1], szIp[2], szIp[3]);

	// 修改相机IP时默认网关与网卡网关相同
	// the default gateway is the same as the network card gateway when modifying the camera IP
	m_ipctrlCameraGateway.SetAddress(szIp[0], szIp[1], szIp[2], szIp[3]);

	// 网卡Mask
	// network card mask
	strIpToNumberIp(sptrGigeInterfaceInfo->_strMaskAddr.c_str(), szIp, 4);	
	m_ipctrlInterfaceMask.SetAddress(szIp[0], szIp[1], szIp[2], szIp[3]);

	// 修改相机IP时默认子网掩码与网卡子网掩码相同
	// the default gateway is the same as the network card gateway when modifying the camera IP
	m_ipctrlCameraMask.SetAddress(szIp[0], szIp[1], szIp[2], szIp[3]);

	// 网卡Ip
	// network card Ip
	strIpToNumberIp(sptrGigeInterfaceInfo->_strIpAddr.c_str(), szIp, 4);	
	m_ipctrlInterfaceIp.SetAddress(szIp[0], szIp[1], szIp[2], szIp[3]);

	// 预填相机IP前三位
	// Pre fill camera IP top three
	m_ipctrlCameraIp.SetAddress(szIp[0], szIp[1], szIp[2], 0);

	// 设置相机Mac
	// set camera Mac
	Dahua::CMvCamera::NetAddressInfoPtr sptrCameraIpInfo = m_sptrMvCamera->getGigECameraInfo();
	if ( NULL != sptrCameraIpInfo )
	{
		m_editCameraMac.SetWindowText((LPCTSTR)sptrCameraIpInfo->_strMac.c_str());
	}	
}

void CForceIpDlg::strIpToNumberIp(const char* pIp, int* pszIp, int iSize)
{
	if ( NULL == pszIp ||
		iSize < 4 )
	{
		return;
	}

	std::string stdstrValue = pIp;
	int i = 0;
	::memset(pszIp, 0, (sizeof(int) * iSize));

	while ( true )
	{
		std::string::size_type iPos = stdstrValue.find( "." );
		if ( iPos == std::string::npos )
		{
			std::stringstream ss;
			ss << stdstrValue;
			int val = 0;
			ss >> val;
			pszIp[i++] = val;

			break;
		}

		std::string strSub = stdstrValue.substr( 0, iPos );
		stdstrValue = stdstrValue.substr( iPos+1 );
		std::stringstream ss;
		ss << strSub;
		int val = 0;
		ss >> val;
		pszIp[i++] = val;

		iPos = stdstrValue.find( "." );
	}
}

BEGIN_MESSAGE_MAP(CForceIpDlg, CDialog)
	ON_BN_CLICKED(IDOK, &CForceIpDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CForceIpDlg 消息处理程序
// CForceIpDlg message processing program 
void CForceIpDlg::OnBnClickedOk()
{
	// 获取界面数据
	// Get interface data
	UpdateData(TRUE);

	TCHAR szBuf[24] = {0};
	m_ipctrlCameraGateway.GetWindowText(szBuf, 24);

#ifdef UNICODE
	std::string strGateway = WStringToString(szBuf);
#else
	std::string strGateway = szBuf;
#endif // UNICODE

	::memset(szBuf, 0, sizeof(szBuf));
	m_ipctrlCameraMask.GetWindowText(szBuf, 24);

#ifdef UNICODE
	std::string strMask = WStringToString(szBuf);
#else
	std::string strMask = szBuf;
#endif // UNICODE

	::memset(szBuf, 0, sizeof(szBuf));
	m_ipctrlCameraIp.GetWindowText(szBuf, 24);

#ifdef UNICODE
	std::string strIp = WStringToString(szBuf);
#else
	std::string strIp = szBuf;
#endif // UNICODE

	if ( NULL == m_sptrMvCamera )
	{
		AfxMessageBox(_T("Force Ip failed!"));
		return;
	}

	bool bRet = m_sptrMvCamera->forceIpAddress(strIp.c_str(), strMask.c_str(), strGateway.c_str());
	if ( !bRet )
	{
		AfxMessageBox(_T("Force Ip failed!"));
		return;
	}

	OnOK();
}
